package com.kayt.cyberBar.cuser.business.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.cuser.business.dao.ITaskCircleDao;
import com.kayt.cyberBar.cuser.business.entity.po.BTaskCircle;

@Repository("taskCircleDao")
public class TaskCircleDaoImpl implements ITaskCircleDao {
	@Autowired
	private EntityDao entityDao;

	@Override
	public void saveOrUpdate(BTaskCircle entity) throws SQLException {
		if (entity.getId() == null) {
			entityDao.save(entity);
		} else {
			entityDao.update(entity);
		}
	}

	@Override
	public BTaskCircle getByCode(String taskCode) throws SQLException {
		return entityDao.getObj(BTaskCircle.class, "o.taskCode = '" + taskCode + "'", null);
	}

	@Override
	public void delete(BTaskCircle entity) {
		entityDao.delete(entity);
	}

	@Override
	public PageData<BTaskCircle> findByModelCode(Integer pageNum,Integer pageSize,String modelCode) throws SQLException {
		String hql = StringUtils.isEmpty(modelCode) ? " o.status != '00040000000446320171011295344657'"
				: ("o.modelCode = '" + modelCode + "' and o.status != '00040000000446320171011295344657' order by o.taskStartTime asc");
		return entityDao.getPagingData(BTaskCircle.class, pageNum, pageSize, hql, null, null);
	}

	@Override
	public BTaskCircle getById(String id) throws SQLException {
		return entityDao.findById(Long.parseLong(id), BTaskCircle.class);
	}

	@Override
	public List<BTaskCircle> findByModelTask(String statusCode) throws SQLException {
		String sql = "select * from b_task_circle tc ,b_task_model tm where tc.MODEL_CODE = tm.TASK_MODEL_CODE and tm.STATUS_CODE = '"
				+ statusCode + "' and tc.STATUS = '" + statusCode + "'";
		return entityDao.getPagingDataBySql(BTaskCircle.class, -1, -1, sql, null);
	}

	@Override
	public List<BTaskCircle> findByNotModelTask() throws SQLException {
		return entityDao.getDataByHql(-1, -1,
				"select o from BTaskCircle o where ((o.modelCode is null) or (o.modelCode = ''))", null);
	}

}
